package com.lagou.studentsystem.servlet;

import com.lagou.studentsystem.model.Student;
import com.lagou.studentsystem.service.ClassesService;
import com.lagou.studentsystem.service.StudentService;
import com.lagou.studentsystem.service.impl.ClassesServiceImpl;
import com.lagou.studentsystem.service.impl.StudentServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;


@WebServlet(name = "RemoveClassesServlet",urlPatterns = "/removeClasses")
public class RemoveClassesServlet extends HttpServlet {

   private StudentService studentService = new StudentServiceImpl();
   private ClassesService classesService = new ClassesServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String c_id = request.getParameter("c_id");
        //首先要查询是否有该班级是否还有学生
        List<Student> studentByClassId = studentService.getStudentByClassId(c_id);
        System.out.println(studentByClassId);
        if (studentByClassId!=null && !studentByClassId.isEmpty()){
            request.setAttribute("errmsg","删除失败，该班级还有学生");
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }else {
            System.out.println("2");
            int i = classesService.removeClassesById(c_id);
            RequestDispatcher findClassesAll = request.getRequestDispatcher("findClassesAll");
            findClassesAll.forward(request,response);

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
